<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.id?$t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="520px" @close='close' top="10vh">
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" label-width="80px" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item :label="$t('名称')" prop="name">
          <el-input v-emoji v-model="record.name" size='small' ref="name"></el-input>
        </el-form-item>
        <el-form-item :label="$t('描述')" prop="desc">
          <el-input v-emoji v-model="record.desc" size='small' ref="desc" type="textarea" @keydown.native="handleKeyCode($event)" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
        </el-form-item>
        <el-form-item :label="$t('电话')" prop="phone">
          <el-input v-emoji v-model="record.phone" size='small' ref="phone"></el-input>
        </el-form-item>
        <el-form-item label="Email" prop="mail">
          <el-input v-emoji v-model="record.mail" size='small' ref="mail" :placeholder="$t('多个Email以,隔开')"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addResponsible, editResponsible, getResponsibleInfo } from '@/api/list/assets/responsible'
import { inputClass, texttextClass } from '@/utils/input-class.js'
export default {
  name: 'ResponsibleDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateEmail = (rule, value, callback) => {
      let regexp = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
      let list = value.split(',')
      for (let item of list) {
        if (value !== '' && !regexp.test(item)) {
          callback(new Error(this.$t('Email格式错误')))
        }
      }
      callback()
    }
    var validatePhone = (rule, value, callback) => {
      var myreg = /^[1][0-9]{10}$/ // 移动
      var myreg2 = /^(([0+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/ // 固话
      if (value && !(myreg.test(value) || myreg2.test(value))) {
        callback(new Error(this.$t('电话格式不正确')))
      } else {
        callback()
      }
    }

    return {
      dialogVisible: false,
      record: {
        id: null,
        name: '',
        desc: '',
        phone: '',
        mail: ''
      },
      recordCopy: {
        id: null,
        name: '',
        desc: '',
        phone: '',
        mail: ''
      },
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        mail: [{ required: false, validator: validateEmail, trigger: 'blur' }],
        phone: [{ required: false, validator: validatePhone, trigger: 'blur' }]
      }
    }
  },
  watch: {
    'record.desc': {
      handler (newVal) {
        this.record.desc = newVal.replace(/[\r\n]/g, '')
      },
      immediate: true
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      if (data) {
        getResponsibleInfo({ name: data.name }).then(res => {
          this.record = Object.assign(this.record, res.info[0])
        })
      }
      this.$nextTick(() => {
        const data = ['name', 'phone', 'mail']
        const textdata = ['desc']
        inputClass(this.$refs, data)
        texttextClass(this.$refs, textdata)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.record.id) {
            addResponsible(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
              }
              this.close()
              this.$parent.listData()
            })
          } else {
            editResponsible(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('编辑成功'),
                  type: 'success'
                })
              }
              this.close()
              this.$parent.listData()
            })
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    handleKeyCode (event) {
      if (event.keyCode === 13) {
        event.preventDefault() // 阻止浏览器默认换行操作
        return false
      }
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(80vh - 184px);
  overflow-y: auto;
}
</style>
